package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    private final CategoryBrandService categoryBrandService;
    @Autowired
    public  CategoryServiceImpl (CategoryBrandService categoryBrandServic){
        this.categoryBrandService = categoryBrandServic;
    }

    @Override
    public List<CategoryDTO> queryCategoryByBrandId(Long brandId) {
        // 1.根据品牌id，查询中间表，得到中间表对象集合
        List<CategoryBrand> brand_id = categoryBrandService.query().eq("brand_id", brandId).list();
        if (CollectionUtils.isEmpty(brand_id)){
            //为空返回
            return Collections.emptyList();
        }




        //获取分类集合id
        List<Long> collect = brand_id.stream().map(CategoryBrand::getBrandId).collect(Collectors.toList());

        List<Category> categories = listByIds(collect);

        return CategoryDTO.convertEntityList(categories);
    }
}
